<?php
/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 7/18/2016
 * Des: 系统管理员
 */
defined('INHkMvc') or exit('Access Invalid!');

class App_goodModel extends AppModel
{
    public function addPlanGood($data)
    {
        unset($data['id']);
        $good_id = $this->field('id')->find(['num_iid' => $data['num_iid']]);
        if (intval($good_id['id']) == 0) {
            $res = $this->insert($data);
        } else {
            unset($data['add_time']);
            $data['update_time'] = time();
            $res = $this->update($data, ['id' => intval($good_id['id'])]);
        }
        if ($res === false) {
            return false;
        }
        return $good_id['id'] == 0 ? $res : $good_id['id'];
    }

    public function getGoodTkRateByNumId($num_iid)
    {
        $tk_rate = $this->field('tk_rate')->find(['num_iid' => $num_iid]);
        if (isset($tk_rate) && $tk_rate != NULL) {
            return $tk_rate['tk_rate'];
        } else {
            return false;
        }
    }

    public function updateDxjhPlanGood(){
        return DB::query('update mp_good, mp_good_plan set mp_good.`status`=0 where mp_good.dxjh_id = mp_good_plan.campaignId and mp_good_plan.`task_status`=0');
    }
    public function updateGoodByComcampaignId($campaignId)
    {
        $res = $this->update(['is_update' => 0], ['dxjh_id' =>$campaignId]);
        if ($res === false) {
            return false;
        } else {
            return true;
        }
    }
    public function updateStatusByComcampaignId($comcampaignId)
    {
        return DB::query('update mp_good set mp_good.`status` = mp_good.`is_update` where mp_good.dxjh_id ='.$comcampaignId);
    }
    public function getGoodDefineTkRateBySellerId($num_iid){
        if($this->find(['num_iid'=>$num_iid])){
            return $this->find(['num_iid'=>$num_iid])['define_tk_rate'];  
        }else{
            return $define_tk_rate=0;
        }
    }
    public function getGoodRateTimeBySellerId($num_iid){
        if($this->find(['num_iid'=>$num_iid])){
            return $this->find(['num_iid'=>$num_iid])['rate_time'];  
        }else{
            return time();
        }
    }
    public function getMemberGoodClickUrl($member_id, $good_id)
    {
        $site_data=M('app.ali_app')->getAllSiteName();
        if(empty($site_data)){
            return false;
        }
        $i=0;
        while ($i<count($site_data)) {
            $member_url = M('app.good_2_vip')->field('short_url,url,coupon_url,id,site_name')->find(['member_id' => $member_id, 'good_id' => $good_id,'site_name' => $site_data[$i]['app'],'site_id' => $site_data[$i]['id']]);
            if (!empty($member_url['short_url']) || !empty($member_url['url']) || !empty($member_url['coupon_url'])) {
                break;
            }else{
                $i++;
            }
            if($i>=count($site_data)){
                return false;
            }
        }
        if(empty($member_url)){
            return false;
        }else{
           $data=$this->getMemberGoodClickUrlByData($member_url);
           return $data;
        }
    }
    public function getMemberGoodClickUrlByData($member_url)
    {   
        $data=[];
        $data['site_name']=$member_url['site_name'];
        $data['id']=$member_url['id'];
        if(!empty($member_url['coupon_url'])){
            $data['click_url']=$member_url['coupon_url'];
            return $data;
        }else if (!empty($member_url['short_url'])) {
            $data['click_url']=$member_url['short_url'];
            return $data;
        } else {
            $click_url=getShortClickUrlByUrl($member_url['url'],$member_url['site_name']);
            if($click_url==false){
                $data['click_url']=$member_url['url'];
                return $data;
            }else{
                M('app.good_2_vip')->update(['short_url'=>$click_url],['id' =>intval($member_url['id'])]);
                $data['click_url']=$click_url;
                return $data;
            }
        }
    }
    public function getMemberGoodClickUrlForExcel($member_id, $good_id)
    {
        $data=$this->getMemberGoodClickUrl($member_id, $good_id);
        if(!empty($data)){
            return $data;
        }else{
            $num_iid = $this->field('num_iid')->find(['id' => $good_id])['num_iid'];
            $site_data=M('app.ali_app')->getAllSiteName();
            if(empty($site_data)){
                return false;
            }
            $data=M('app.good_2_vip')->getCollectUrlByAllSite($num_iid,$good_id,$member_id,$site_data);
            if(empty($data)){
                return false;
            }else{
                return $data;
            }
        }
    }
    public function getMemberGoodTbPassForExcel($member_id, $good_id,$pict_url, $title)
    {
        $data=$this->getMemberGoodClickUrlForExcel($member_id, $good_id);
        if(!empty($data)){
            $shareTpwd=getShareTpwd($pict_url, $title,$data['click_url'],$data['site_name']);
            if($shareTpwd!=false){
                return $shareTpwd;
            }else{
               return false;
            }
        }else{
            return false;
        }
    }
    public function addPlansGood($data){
        $res=$this->insert($data);   
        if ($res === false) {
          return false;
        }else{
            if(intval($data['seller_id'])!=0){
                $this->updateAdminSeller($data['seller_id']);
            } 
          return $res;  
        }     
    }
    public function updateAdminSeller($seller_id){
        $where = [ 'is_delete' => 0,
                    'status' => 1,
                    'seller_id'=>$seller_id
        ];
        $data=$this->field('nick,shop_name')->find($where);
        if(!empty($data)){
            if(!M('app.admin_seller')->find(['tb_seller_id'=>$seller_id])){
                M('app.admin_seller')->insert(['tb_seller_id'=>$seller_id,'tb_seller_name'=>$data['nick'],'tb_shop_name'=>$data['shop_name']]);
            } else{
                M('app.admin_seller')->update(['tb_seller_id'=>$seller_id,'tb_shop_name'=>$data['shop_name'],'is_compare'=>1,'tb_seller_name'=>$data['nick'],'status'=>1],['tb_seller_id'=>$seller_id]);
            }
        }
    }
}
